<?php
namespace pgc;
use \hc\tpl;
use \hc\session;
use \hc\js;
class Column extends tpl{
	public function __construct(){
		parent::__construct();
		session::init();
		$this->column_m=new \model\column_m;
		if(isset($_SESSION['pgcinfo'])){
				$this->pgcinfo=$_SESSION['pgcinfo'];
		}else{
				$url=MODEL_URL."/Login/index.html";
			 echo 	js::to_url($url);exit;
		}
	}
	public function index(){
      $data=$this->column_m->s_all_ajaxs();
			if(isset($_POST['ajaxs'])){
					ajax_return(true,'',$data);
			}
      $this->assign('listarr',$data['list']);
      $this->assign('pageshow',$data['pageshow']);
			$this->display('column/lists.tpl');
	}
  //添加栏目
  public function columnAdd(){
    if(isset($_POST['ajaxs'])){
      $data=ajaxs_from($_POST['arr']);
      if(!check_regex($data['colcod'],'isnozh')){
          ajax_return(false,'栏目代码:只能为字母和数字');
      }elseif(trim($data['colname'])==''){
          ajax_return(fasle,'栏目名称不能为空');
      }
			//栏目唯一性验证
			$where_unique['colcod']=array('=',$data['colcod']);
			if($this->column_m->s_one($where_unique)){
				ajax_return(false,'栏目代码已经存在：'.$data['colcod']);
			}
			$where_unique2['colname']=array('=',$data['colname']);
			if($this->column_m->s_one($where_unique2)){
				ajax_return(false,'栏目名称已经存在：'.$data['colname']);
			}

      $data['addtime']=time();
      if(($roleid=$this->column_m->a_one($data))!==false){
          addlog($this->pgcinfo,'成功','创建栏目id:'.$roleid);
          ajax_return(true,'添加栏目成功');
      }else{
          addlog($this->pgcinfo,'失败','创建栏目');
          ajax_return(false,'添加栏目失败');
      }
    }
      $this->display('column/add.tpl');
  }
  //编辑栏目
  public function columnEdit(){
    if(isset($_POST['ajaxs'])){
        $data=ajaxs_from($_POST['arr']);
        $where['id']=array('=',$data['id']);
				//栏目唯一性验证
				$where_unique['colcod']=array('!=',$data['colcod']);
				$where_unique['colname']=array('=',$data['colname']);
				if($this->column_m->s_one($where_unique)){
					ajax_return(false,'栏目名称已经存在：'.$data['colname']);
				}
				//end
        if($this->column_m->e_one($where,$data)===false){
          addlog($this->pgcinfo,'失败','编辑栏目id:'.$data['id']);
          ajax_return(false,'编辑栏目失败');
        }else{
          addlog($this->pgcinfo,'成功','编辑栏目id:'.$data['id']);
          ajax_return(true,'编辑栏目成功');
        }
    }else if(isset($_GET['id'])){
        $where['id']=array('=',$_GET['id']);
        $list=$this->column_m->s_one($where);

        $this->assign('list',$list);
        $this->display('column/edit.tpl');
    }
    echo js::alert('不存在栏目编辑id值');
  }
  //删除栏目
  public function columnDel(){
      if(isset($_POST['id'])){
          $where['id']=array('=',$_POST['id']);
          if($this->column_m->d_one($where)===false){
              addlog($this->pgcinfo,'失败','删除栏目id:'.$_POST['id']);
              ajax_return(false,'删除失败');
          }else{
              addlog($this->pgcinfo,'成功','删除栏目id:'.$_POST['id']);
              ajax_return(true,'删除成功');
          }
      }
  }
}
